package com.veng.springbootdemo.service;

import com.veng.springbootdemo.dao.UserRepository;
import com.veng.springbootdemo.utils.EmailUtils;
import com.veng.springbootdemo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImp implements UserService{
    @Value("${com.veng.title}")
    private String creator;

    @Autowired
    private UserRepository userRepository;
    @Override
    public User getUser() {
        User user = new User();
        user.setUsername(creator);
        user.setAge(22);
        return user;
    }

    @Override
    public void login(HttpSession session,String username,String password) throws UserNotExistException, PasswordErrorException, UsernameEmptyException, PasswordEmptyException {
        if (username.isEmpty()) {
            throw new UsernameEmptyException();
        }
        if (password.isEmpty()) {
            throw new PasswordEmptyException();
        }
        com.veng.springbootdemo.po.User byUserName = userRepository.findByUserName(username);
        if (byUserName == null) {
            throw new UserNotExistException();
        }else{
            String correct_password = byUserName.getPassword();
            if (correct_password.equals(password)) {
                session.setAttribute("login",true);
            }else{
                throw new PasswordErrorException();
            }
        }
    }

    @Override
    public void register(String username, String password,String email) throws UsernameEmptyException, PasswordEmptyException, EmailEmptyException, EmailInvalidException, UserExistException, EmailExistException {
        if (username.isEmpty()) {
            throw new UsernameEmptyException();
        }
        if (password.isEmpty()) {
            throw new PasswordEmptyException();
        }
        if (email.isEmpty()) {
            throw new EmailEmptyException();
        }

        boolean verify = EmailUtils.verify(email);
        if (!verify) {
            throw new EmailInvalidException();
        }
        com.veng.springbootdemo.po.User byUserName = userRepository.findByUserName(username);
        if (byUserName != null) {
            throw new UserExistException();
        }
        com.veng.springbootdemo.po.User byEmail = userRepository.findByEmail(email);
        if (byEmail != null) {
            throw new EmailExistException();
        }
        userRepository.save(new com.veng.springbootdemo.po.User(username,password,email));

    }

    /**
     * 分页获取用户 dev
     * 注意 页数从 0 开始
     * @return
     */
    @Override
    public List<com.veng.springbootdemo.po.User> findAll(int page, int size) {
//        int page = 0,size = 10;

        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page-1, size, sort);
        Page<com.veng.springbootdemo.po.User> all = userRepository.findAll(pageable);
        System.out.println(all.getSize());
        return all.getContent();
    }

    @Override
    public com.veng.springbootdemo.po.User findFirstByUsername() {
        return userRepository.findFirstByOrderByUserNameAsc();
    }

    @Transactional
    @Override
    public void modifyPasswordByUsername(String username, String password) {
        userRepository.modifyPasswordByUsername(username,password);
    }

    public class UserNotExistException extends Exception {

    }

    public class PasswordErrorException extends Exception {
    }

    public class UsernameEmptyException extends Exception {
    }

    public class PasswordEmptyException extends Exception {
    }

    public class EmailEmptyException extends Exception {

    }

    public class EmailInvalidException extends Exception {
    }

    public class UserExistException extends Exception {

    }

    public class EmailExistException extends Exception {
    }
}
